package com.example.xyxwebbackend001.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.example.xyxwebbackend001.common.CommonResult;
import com.example.xyxwebbackend001.model.DTO.EditProfileDTO;
import com.example.xyxwebbackend001.model.DTO.EmailUpdateDTO;
import com.example.xyxwebbackend001.model.entity.TPosts;
import com.example.xyxwebbackend001.model.vo.PartUserInfoVO;
import com.example.xyxwebbackend001.model.vo.SecurityInfoVO;
import com.example.xyxwebbackend001.model.vo.UserProfileVO;
import com.example.xyxwebbackend001.service.EmailService;
import com.example.xyxwebbackend001.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserCenterController {
    @Autowired
    private UserCenterService userCenterService;

    @Autowired
    private EmailService emailService;


    @GetMapping("/part-info")
    public CommonResult<PartUserInfoVO> getPartUserInfo(@RequestParam Integer userId) {
        PartUserInfoVO partUserInfoVO = userCenterService.getPartUserInfo(userId);
        return CommonResult.success(partUserInfoVO);
    }
    @GetMapping("/profile")
    public CommonResult<UserProfileVO> getUserProfile(@RequestParam Integer userId) {
        UserProfileVO userProfileVO = userCenterService.getUserProfile(userId);
        return CommonResult.success(userProfileVO);
    }
    @GetMapping("/avatar/path")
    public CommonResult<Object> getUserAvatarPath(@RequestParam Integer userId) {
        String avatarPath = userCenterService.getUserAvatarPath(userId);
        return CommonResult.success(avatarPath);
    }
    @PostMapping("/avatar/upload")
    public CommonResult<String> uploadAvatar(
            @RequestParam("userId") Integer userId,
            @RequestParam("avatar") MultipartFile file) throws IOException {

        String newPath = userCenterService.updateUserAvatar(userId, file);
        return CommonResult.success(newPath);
    }
    @SaCheckLogin
    @PutMapping("/profile/edit")
    public CommonResult<Object> updateUserProfile(
            @RequestBody EditProfileDTO editProfileDTO) {

        // 通过Sa-Token获取当前登录用户ID
        int userId = StpUtil.getLoginIdAsInt();

        userCenterService.updateUserProfile(
                userId,
                editProfileDTO.getSex(),
                editProfileDTO.getSignature(),
                editProfileDTO.getProfile(),
                editProfileDTO.getCityId(),
                editProfileDTO.getInterestedCulture()
        );
        return CommonResult.success("修改成功");
    }
    @GetMapping("/t-posts/list")
    public CommonResult<List<TPosts>> getUserTPostsList(@RequestParam Integer userId) {
        List<TPosts> TPostLists = userCenterService.getUserTPosts(userId);
        return CommonResult.success(TPostLists);
    }

    @PutMapping("/t-post/delete")
    public CommonResult<Object> deleteTPostById(@RequestParam Integer tPostId) {
        Integer result = userCenterService.deleteTPostById(tPostId);
        if (result == 0) {
            return CommonResult.tip("删除失败");
        }
        return CommonResult.tip("删除成功");
    }

    @SaCheckLogin
    @GetMapping("/search/security-info")
    public CommonResult<Object> securityCheck() {
        SecurityInfoVO securityInfoVO = userCenterService.getSecurityInfo(StpUtil.getLoginIdAsInt());
        return CommonResult.success(securityInfoVO.getFieldStatus());
    }
    @SaCheckLogin
    @PutMapping("/edit/security-email")
    public CommonResult<Object> updateEmail(@RequestBody EmailUpdateDTO emailUpdateDTO) {
        String email = emailUpdateDTO.getEmail();
        String code = emailUpdateDTO.getCode();
        Integer userId = StpUtil.getLoginIdAsInt();
        if (!emailService.validateCode(email, code)) {
            return CommonResult.fail(400, "验证码错误或已过期");
        }
        userCenterService.updateEmail(userId, email);
        return CommonResult.success("邮箱修改成功");
    }
}
